<template>
  <el-container class="layout-container">
    <el-aside width="220px" class="aside">
      <div class="logo">
        <img src="../assets/logo.svg" alt="Logo" />
        <span>租赁管理系统</span>
      </div>
      <el-scrollbar>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="1"  @click="select">
            <el-icon><House /></el-icon>
          <span>首页</span>
        </el-menu-item>
        <el-sub-menu index="2">
          <template #title>
            <el-icon><location /></el-icon>
            <span>公寓信息</span>
          </template>
            <el-menu-item index="2-1" >租赁管理</el-menu-item>
            <el-menu-item index="2-2" @click="handleChooseRoom">房间信息管理</el-menu-item>
            <el-menu-item index="2-3">支付方式管理</el-menu-item>
            <el-menu-item index="2-4">文件管理</el-menu-item>
            <el-menu-item index="2-5">房间杂费管理</el-menu-item>
            <el-menu-item index="2-6" @click="propertiesApartment">属性管理</el-menu-item>
            <el-menu-item index="2-7" @click="messageApartment">公寓信息管理</el-menu-item>
            <el-menu-item index="2-8">地区信息管理</el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="3">
          <template #title>
            <el-icon><User /></el-icon>
            <span>租赁管理</span>
        </template>

        <el-menu-item index="3-1">租约管理</el-menu-item>
        <el-menu-item index="3-2">预约看房管理</el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="4">
          <template #title>
            <el-icon><User /></el-icon>
            <span>用户管理</span>
        </template>
        <el-menu-item index="4-1" @click="userList">用户列表 </el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="5">
        <template #title>
          <el-icon><User /></el-icon>
          <span>系统信息管理</span>
        </template>
          <el-menu-item index="5-1" @click="systemUser">用户信息管理</el-menu-item>
            <el-menu-item index="5-2">用户岗位管理</el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="6">
        <template #title>
          <el-icon><User /></el-icon>
          <span>平台用户管理</span>
        </template>
        </el-sub-menu>
        </el-menu>
      </el-scrollbar>
    </el-aside>

    <el-container>
      <el-header class="header">
        <div class="header-left">
          <el-icon class="toggle-sidebar" @click="toggleSidebar">
            <Fold v-if="!isCollapse" />
            <Expand v-else />
          </el-icon>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar :size="32" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
              <span class="username">{{ username }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人信息</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main class="main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { Menu as Fold, Expand, House } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const isCollapse = ref(false)
const activeMenu = ref('1')
const username = ref(localStorage.getItem('username') || '管理员')

 function select() {
  router.push('/home/page')
}
 function messageApartment() {

  router.push('/home/message')
}
 function propertiesApartment() {
  router.push('/home/properties')
}
function handleChooseRoom (){
router.push('/home/roomPage')
}
function systemUser (){
router.push('/system/systemUser')
}
onMounted(() => {
  select()
  if(router.currentRoute.value.path === '/'){
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  }
})
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  router.push('/')
}
const userList = () => {
  router.push('/home/userMessage')
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  background-color: #f0f2f5;
}

.aside {
  background-color: #304156;
  transition: width 0.3s;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px solid #1f2d3d;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 12px;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.toggle-sidebar {
  font-size: 20px;
  cursor: pointer;
  color: #666;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
  font-size: 14px;
  color: #666;
}

.main {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-menu {
  border-right: none;
}

:deep(.el-menu-item) {
  &:hover {
    background-color: #263445 !important;
  }
  &.is-active {
    background-color: #1890ff !important;
  }
}
</style>
